﻿
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Windows.Controls;

namespace CommonProjects.AntiFake
{

    [StructLayout(LayoutKind.Sequential)]
    public struct FaceFeatureRectangle
    {
        /// <summary>
        /// 左边
        /// </summary>
        public int left;

        /// <summary>
        /// 上边
        /// </summary>
        public int top;

        /// <summary>
        /// 右边
        /// </summary>
        public int right;

        /// <summary>
        /// 下边
        /// </summary>
        public int bottom;
    }

    [StructLayout(LayoutKind.Sequential)]
    public struct QSFacePointsModel
    {
        public float roll;
        public float yaw;
        public float pitch;
        [MarshalAs(UnmanagedType.ByValArray, SizeConst = 51, ArraySubType = UnmanagedType.I1)]
        public FaceFeaturePoint[] points;
    }

    [StructLayout(LayoutKind.Sequential)]
    public struct FaceFeaturePoint
    {
        public int x;
        public int y;
    }

    public class QSFacePointsFeatureResult
    {
        public QSFacePointsFeatureResult() { }


        public float scole { get; set; }

        public QSFacePointsModel facePoints { get; set; }
    }
    public class WisFacePointsAPI : UserControl
    {
        #region static methods

        [DllImport("wisfacepoints_antifake.dll", CallingConvention = CallingConvention.Cdecl)]
        public static extern int CreateFacePointsEngine();

        [DllImport("wisfacepoints_antifake.dll", CallingConvention = CallingConvention.Cdecl)]
        public static extern float CalculateFacePoints(int engine, byte[] rebdata24, int width, int height, int widthstep, FaceFeatureRectangle rtFace,
             out QSFacePointsModel fpm);

        #endregion

        #region 构造函数

       

        #endregion

        #region 变量

        protected int _engine = -1;


        protected bool _isInit = false;

        #endregion

        #region 公共方法

        public void Init()
        {
            if (!_isInit)
            {
                try
                {
                   _engine = CreateFacePointsEngine();
                   _isInit = true;
                }
                catch (Exception ex)
                {
 
                }
                
            }
            
        }





        public QSFacePointsFeatureResult DetectFacesFeature(byte[] rebdata24, int width, int height, int widthstep, FaceFeatureRectangle rtFace)
        {

            try
            {

                QSFacePointsModel facePoint = new QSFacePointsModel();
                var score = CalculateFacePoints(_engine, rebdata24.Skip(54).Take(rebdata24.Length - 54).ToArray(),
                    width, height, widthstep, rtFace, out facePoint);

                QSFacePointsFeatureResult result = new QSFacePointsFeatureResult();
                    
                result.facePoints = facePoint;
                result.scole = score;
                return result;
                
            }
            catch (Exception ex)
            {
                
            }
          
            return null;
        }


        //public static object BytesToStruct(byte[] bytes, Type strcutType)
        //{
        //    int size = Marshal.SizeOf(strcutType);
        //    IntPtr buffer = Marshal.AllocHGlobal(size);
        //    try
        //    {
        //        Marshal.Copy(bytes, 0, buffer, size);
        //        return Marshal.PtrToStructure(buffer, strcutType);
        //    }
        //    finally
        //    {
        //        Marshal.FreeHGlobal(buffer);
        //    }
        //}

        //public static byte[] StructToBytes(object structObj)
        //{
        //    int size = Marshal.SizeOf(structObj);
        //    IntPtr buffer = Marshal.AllocHGlobal(size);
        //    try
        //    {
        //        Marshal.StructureToPtr(structObj, buffer, false);
        //        byte[] bytes = new byte[size];
        //        Marshal.Copy(buffer, bytes, 0, size);
        //        return bytes;
        //    }
        //    finally
        //    {
        //        Marshal.FreeHGlobal(buffer);
        //    }
        //}

        #endregion

    

       

        public bool IsCreateEngineSuccess
        {
            get { return _isInit; }
        }

        public void Dispose()
        {
            //throw new NotImplementedException();
        }

        
    }
}
